<?php
namespace c\file;
/**
 * Description of file
 *
 * @author YeFng
 */
class map extends \control
{
    // 数据表
    var $table = 'file_map';
    // 种类
    var $type = '';
	
	function __construct($t1, $b='')
    {
        $t1 && $this->table = $t1;
		$b && $this->type = $b;
        parent::__construct();
    }
    
    // add
    function add($id, $a)
    {
        foreach ($a as $v)
         {
            $d = array('data_id'=>$id, 'path'=>$v);
            $this->type && $d['type'] = $this->type;
            if ($this->db->save($this->table, $d))
                \log::write('file_map', 'add', $v);
            else{
                $w = 'data_id='.$id . 'AND path="'. $this->db->escape($v) .'"';
                $this->type && $w .=" AND `type`='$this->type'";
                $r = $this->db->getOne($this->table, '*', $w);
                if(!$r)
                    return false;
             }
         }
        return true;
    }
    function del($id, $a=null)
    {
        $w = "data_id IN($id)";
        if ($a){
            $w .= ' AND path IN("'. $this->db->escape_a($a) .'")';
        }
        $this->type && $w .=" AND `type`='$this->type'";
        \log::write('file_map', 'del', $w);
        return $this->db->del($this->table, $w);
    }
    function get($id)
    {
        $w = 'data_id='.$id;
        $this->type && $w .=" AND `type`='$this->type'";
        $r = $this->db->get($this->table, '*', $w);
        foreach ($r as $k=>$v)
         {
            $n = explode('/', $v['path']);
            $v['name'] = array_pop($n);
            $v['url'] = '?u=cfile\v&q='. \encrypt::q_ed($v['path']) .'&'. basename($v['path']);
            $r[$k] = $v;
         }
        return $r;
    }
}
